/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controls;

import com.aptech.bluepumpkin.Event;
import com.aptech.bluepumpkin.Prize;
import com.aptech.bluepumpkin.Registration;
import com.aptech.bluepumpkin.Winner;
import com.aptech.bluepumpkin.beans.EventFacade;
import com.aptech.bluepumpkin.beans.PrizeFacade;
import com.aptech.bluepumpkin.beans.RegistrationFacade;
import com.aptech.bluepumpkin.beans.WinnerFacade;
import java.io.IOException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import utils.Uility;

/**
 *
 * @author Chien
 */
@ManagedBean
@SessionScoped
public class PrizeControl implements Serializable {

    @EJB
    private EventFacade eventFacade;
    @EJB
    private PrizeFacade prizeFacade;
    @EJB
    private WinnerFacade winnerFacade;
    @EJB
    private RegistrationFacade registrationFacade;
    private Prize prize;
    private Prize selectedPrize;
    private Event event;
    private Event previousEvent;
    private String id;
    UserControl userControl;

    public Prize getSelectedPrize() {
        return selectedPrize;
    }

    public void setSelectedPrize(Prize selectedPrize) {
        this.selectedPrize = selectedPrize;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
        System.out.println("-----set ID: " + id);
    }

    public String decodeString(String text) {
        return Uility.decodeString(text);
    }

    /**
     * Get List<Prize> in a month ago.
     *
     * @return
     */
    public List<Prize> prizeListWithinAmonth() {

        return prizeFacade.getPrizeListWinthinAmonth();
    }

    public Registration selectRegistrationByPrize(Prize prize) {

        return null;
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
        if (null != event) {
            System.out.println("---------set event: " + event.getTitle());
        } else {
            System.out.println("---------set event: ");
        }
    }

    public Date getCurrentDate() {

        return new Date();
    }

    public String formatDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        return sdf.format(date);
    }

    public Prize getPrize() {
        return prize;
    }

    public void setPrize(Prize prize) {
        this.prize = prize;
    }

    /**
     * Creates a new instance of PrizeControl
     */
    public PrizeControl() {
        prize = new Prize();
        event = new Event();
        selectedPrize = new Prize();
        System.out.println("--------PrizeControl constructor-----------");


    }

    public List<Prize> getAllPrize() {
        return prizeFacade.getAllPrizes();
    }

    public List<Event> getAllEventsWithPrizes() {
        // return prizeFacade.getAllEventsWithPrize();
        return eventFacade.getAllActiveEventsWithPrizes();
    }

    public void redirectToAddPrize() {
        prize = new Prize();
        FacesContext context = FacesContext.getCurrentInstance();
        String path = context.getExternalContext().getRequestContextPath() + "/faces/admin/prizes/addPrize.xhtml";
        utils.Uility.redirectPage(context, null, path);
    }

    public void redirectToListrizes() {
        FacesContext context = FacesContext.getCurrentInstance();
        String path = context.getExternalContext().getRequestContextPath() + "/faces/admin/prizes/prizesPage.xhtml";
        utils.Uility.redirectPage(context, null, path);
    }

    public void update(Prize prize) {
        selectedPrize = prize;
        previousEvent = selectedPrize.getEvent();
        redirectToEditPrizes();
    }

    public void redirectToEditPrizes() {
        FacesContext context = FacesContext.getCurrentInstance();
        String path = context.getExternalContext().getRequestContextPath() + "/faces/admin/prizes/editPrize.xhtml";
        utils.Uility.redirectPage(context, null, path);
    }

    public void insert() {
        try {
            prize.setEvent(event);
            prizeFacade.create(prize);
            event.getPrizeList().add(prize);
            eventFacade.edit(event);

            prize = new Prize();
            event = new Event();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Create Success", ""));
            redirectToListrizes();
        } catch (Exception ex) {
            Logger.getLogger(PrizeControl.class.getName()).log(Level.SEVERE, null, ex);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Insert failed", ""));
        }

    }

    public void edit() {
        try {
            //            selectedPrize.setEvent(getEvent());
            Event newEvent = selectedPrize.getEvent();
            // if change event
            if (!previousEvent.equals(newEvent)) {
                previousEvent.getPrizeList().remove(selectedPrize);
                newEvent.getPrizeList().add(selectedPrize);
                eventFacade.edit(previousEvent);
                eventFacade.edit(newEvent);
            }
            prizeFacade.edit(selectedPrize);
                      redirectToListrizes();
        } catch (Exception ex) {
            Logger.getLogger(PrizeControl.class.getName()).log(Level.SEVERE, null, ex);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Update failed", ""));
        }

    }

    public void delete(Prize prize) {

        selectedPrize = prize;
        try {
            List<Winner> winnerList = prize.getWinnerList();
// if prize is used
            if (null != winnerList && !winnerList.isEmpty()) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Prize is inused. You can not delete", ""));
                return;
            }
            Event event1 = selectedPrize.getEvent();
            // remove prize from event
            event1.getPrizeList().remove(selectedPrize);
            eventFacade.edit(event1);

            prizeFacade.remove(prize);


            redirectToListrizes();
        } catch (Exception ex) {
            Logger.getLogger(PrizeControl.class.getName()).log(Level.SEVERE, null, ex);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Delete failed", ""));
        }

    }


    public List<Event> comlpleteMethod(String query) {
        List<Event> allActiveEvents = eventFacade.getAllActiveEvents();
        List<Event> filter = new ArrayList<Event>();
        for (Event event1 : allActiveEvents) {
            if (event1.getTitle().contains(query)) {
                filter.add(event1);
            }

        }
        return filter;
    }

    
}
